/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.agent

import groovy.transform.CompileStatic
import model.agent.exception.ElementExistedException

/**
 * 抽象主体类，
 * @author lee
 *
 */

@CompileStatic
public class Agent implements AgentInterface{

	String name = ""

	def HashMap<String, AgentVariableInterface> variables = [:]

	def HashMap<String, AgentInterface> subAgents = [:]

	/**
	 * 检查属性是否存在，如果不存在则抛出异常
	 * @param name 属性的名字
	 */
	boolean pExists(String name){
//		println name
//		println variables
		//		println properties.containsKey(name)
		if(!variables.containsKey(name)){
			throw new NoSuchElementException("无此属性${this.name}-${name}")
		}
		//cmds.call()
		return true
	}

	/**
	 * jiancha子主体存在，则执行命令，如果不存在，则抛出异常
	 * @param name 子主体名字
	 * @param cmds 要执行的命令，一个closure
	 */
	def void aExists(String name, Closure cmds ){
		if(!subAgents.keySet().contains(name)){
			throw new NoSuchElementException("无此主体${this.name}-${name}")
		}
		cmds.call()

	}

	//=====================属性系列======================
	
	/**
	 * 返回主体的变量map，key是变量名，value是值
	 */
	HashMap getVarsMap(){
		def np = [:]
		variables.each{key ,AgentVariableInterface value ->
			np[key] = value.getValue()
		}
		return np
	}

	/**
	 * 
	 */
	def void addVariable(AgentVariableInterface p){
		String name = p.name
		if(variables.keySet().contains(name)){
			throw new ElementExistedException("变量\"${this.name}-${name}\"已存在，不能重复添加！")
		}
		variables[name] = p
	}

	/**
	 * 
	 */
	public AgentVariableInterface getVariable(String name){
		return variables[name]
	}

	//	def void removevariable(String name){
	//		pExists(name){ properties.remove(name) }
	//	}

	//标准的setter和getter
	def void setVarValue(String name, int value){
		if (pExists(name)) {
			((AgentVariableInterface)variables[name]).value = value
		}
	}
	
	def void setVarTimestamp(String name, int timestamp){
		if (pExists(name)) {
			((AgentVariableInterface)variables[name]).setTimestamp(timestamp)
		}
	}

	int getVarValue(String name){
		def value = 0
		if (pExists(name)){
			value =  variables[name].value
		}
	}
	
	int getVarTimestamp(String name){
		def timestamp = 0
		if (pExists(name)){
			timestamp =  variables[name].getTimestamp()
		}
		return timestamp
	}

	/**
	 * 所有属性值归零
	 */
	public void resetVars(){
		variables.each{key,AgentVariableInterface p ->
			p.reset()
		}
	}
	//=====================属性系列结束======================


	//=====================主体系列开始======================
	def void addSubAgent(AgentInterface agent){
		String name = agent.name
		if(subAgents.keySet().contains(name)){
			throw new ElementExistedException("主体${this.name}-${name}已存在，不能重复添加！")
		}
		subAgents[name] = agent
	}

	//	def void removeSubAgent(String name){
	//		aExists(name) {subAgents.remove(name)}
	//	}

	public AgentInterface getSubAgent(String name){
		aExists(name){return subAgents[name]}
		return null
	}
	//=====================主体系列结束======================

}

